package service

import (
	"github.com/gin-gonic/gin"
	_ "github.com/tealeg/xlsx"
	"io"
	"project/app/electromechanical/models"
	"project/app/electromechanical/models/bo"
	"project/app/electromechanical/models/dto"
	"project/utils"
)

type Stu struct {
}

// GetStuList 查询学生列表业务逻辑
func (e *Stu) GetStuList(p *dto.GetStuList) (stuList *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	stuList, err = stu.GetStuList(p)
	//return
	res := new(bo.GetStu)
	if p.Size>0 && p.Current>0  {
		stuList,  err := stu.GetStuList(p)
		if err != nil {
			return nil, err
		}
		var getStuList []*bo.GetStuList
		for _, stu := range *stuList {
			getStuList = append(getStuList, &bo.GetStuList{
				Id:         stu.ID,
				Gender:    stu.Gender,
				Major:   stu.Major,
				Class:   stu.Class,
				Name:       stu.Name,
				Number:   stu.Number,
				Phone:   stu.Phone,
				Nation:   stu.Nation,
				Committee:   stu.Committee,
				StudentUnion:  stu.StudentUnion,
				State:   stu.State,
				PsychologicalWarning:  stu.PsychologicalWarning,
				Team:   stu.Team,
			})
		}

		res.Size = int(p.Size)
		res.Current = int(p.Current)
		res.Records = getStuList

	} else if p.Size==0 || p.Current==0 {
		return nil, err
	} else {
		stuList,  err := stu.GetStuList(p)
		if err != nil {
			return nil, err
		}
		var getStuList []*bo.GetStuList
		for _, stu := range *stuList {
			getStuList = append(getStuList, &bo.GetStuList{
				Id:         stu.ID,
				Gender:    stu.Gender,
				Major:   stu.Major,
				Class:   stu.Class,
				Name:       stu.Name,
				Number:   stu.Number,
				Phone:   stu.Phone,
				Nation:   stu.Nation,
				Committee:   stu.Committee,
				StudentUnion:  stu.StudentUnion,
				State:   stu.State,
				PsychologicalWarning:  stu.PsychologicalWarning,
				Team:   stu.Team,
			})
		}
	}
	return
}
//  GetStuDetails通过id查询学生详情
func (e *Stu) GetStuDetails(p *dto.GetStuDetails) (Details *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Details, err = stu.GetStuDetails(p)
	res := new(bo.GetStuDetail)
		stuDetails,  err := stu.GetStuDetails(p)
		if err != nil {
			return nil, err
		}

		var getStuDetails []*bo.GetStuDetail
		for _, stu := range *stuDetails {
			getStuDetails = append(getStuDetails, &bo.GetStuDetail{
				Id:         stu.ID,
				Picture:    stu.Picture,
				Gender:    stu.Gender,
				Major:   stu.Major,
				Place:    stu.Place,
				NativePlace: stu.NativePlace,
				Dormitory: stu.Dormitory,
				WatchmanName:stu.WatchmanName,
				WatchmanRelationship:stu.WatchmanRelationship,
				WatchmanPhone:stu.WatchmanPhone,
				Remarks:stu.Remarks,
				AcademicWarning:stu.AcademicWarning,
				PoliticalOutlook: stu.PoliticalOutlook,
				Class:   stu.Class,
				Name:       stu.Name,
				Number:   stu.Number,
				Phone:   stu.Phone,
				Nation:   stu.Nation,
				Committee:   stu.Committee,
				StudentUnion:  stu.StudentUnion,
				State:   stu.State,
				PsychologicalWarning:  stu.PsychologicalWarning,
				Team:   stu.Team,
			})

		}
		res.Id = p.Id
	return
}
//GetStuNumber 通过学号查询学生列表
func (e *Stu) GetStuNumber(p *dto.GetStuNumber) (Number *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Number, err = stu.GetStuNumber(p)
	res := new(bo.GetStuList)
		stuNumber,  err := stu.GetStuNumber(p)
		if err != nil {
			return nil, err
		}
		var getStuNumber []*bo.GetStuDetail
		for _, stu := range *stuNumber {
			getStuNumber = append(getStuNumber, &bo.GetStuDetail{
				Id:         stu.ID,
				Gender:    stu.Gender,
				Major:   stu.Major,
				Class:   stu.Class,
				Name:       stu.Name,
				Number:   stu.Number,
				Phone:   stu.Phone,
				Nation:   stu.Nation,
				Committee:   stu.Committee,
				StudentUnion:  stu.StudentUnion,
				State:   stu.State,
				PsychologicalWarning:  stu.PsychologicalWarning,
				Team:   stu.Team,
			})
		}
		res.Number = p.Number
		return
}
//GetStuNumber 通过条件查询学生列表
func (e *Stu) GetStuLists(p *dto.GetStuLists) (Lists *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Lists, err = stu.GetStuLists(p)
	res := new(bo.GetStuList)
		stuLists,  err := stu.GetStuLists(p)
		if err != nil {
			return nil, err
		}
		var getStuNumber []*bo.GetStuList
		for _, stu := range *stuLists {
			getStuNumber = append(getStuNumber, &bo.GetStuList{
				Id:         stu.ID,
				Gender:    stu.Gender,
				Major:   stu.Major,
				Class:   stu.Class,
				Name:       stu.Name,
				Number:   stu.Number,
				Phone:   stu.Phone,
				Nation:   stu.Nation,
				Committee:   stu.Committee,
				StudentUnion:  stu.StudentUnion,
				State:   stu.State,
				PsychologicalWarning:  stu.PsychologicalWarning,
				Team:   stu.Team,
			})
		}
		res.Year = p.Year
		return
}
//GetStuTeam 通过球队查询学生列表
func (e *Stu) GetStuTeam(p *dto.GetStuTeam) (Team *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Team, err = stu.GetStuTeam(p)
	res := new(bo.GetStuList)
	stuNumber,  err := stu.GetStuTeam(p)
	if err != nil {
		return nil, err
	}
	var getStuTeam []*bo.GetStuList
	for _, stu := range *stuNumber {
		getStuTeam = append(getStuTeam, &bo.GetStuList{
			Id:         stu.ID,
			Gender:    stu.Gender,
			Major:   stu.Major,
			Class:   stu.Class,
			Name:       stu.Name,
			Number:   stu.Number,
			Phone:   stu.Phone,
			Nation:   stu.Nation,
			Committee:   stu.Committee,
			StudentUnion:  stu.StudentUnion,
			State:   stu.State,
			PsychologicalWarning:  stu.PsychologicalWarning,
			Team:   stu.Team,
		})
	}
	res.Team=p.Team
	return
}
//GetStuTeam 通过球队查询学生列表
func (e *Stu) GetStuWarning(p *dto.GetStuWarning) (Warning *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Warning, err = stu.GetStuWarning(p)
	res := new(bo.GetStuList)
	stuWarning,  err := stu.GetStuWarning(p)
	if err != nil {
		return nil, err
	}
	var getStuWarning []*bo.GetStuList
	for _, stu := range *stuWarning {
		getStuWarning = append(getStuWarning, &bo.GetStuList{
			Id:         stu.ID,
			Gender:    stu.Gender,
			Major:   stu.Major,
			Class:   stu.Class,
			Name:       stu.Name,
			Number:   stu.Number,
			Phone:   stu.Phone,
			Nation:   stu.Nation,
			Committee:   stu.Committee,
			StudentUnion:  stu.StudentUnion,
			State:   stu.State,
			PsychologicalWarning:  stu.PsychologicalWarning,
			Team:   stu.Team,
		})
	}
	res.PsychologicalWarning = p.PsychologicalWarning
	return
}
//GetStuTeam 通过班委查询学生列表
func (e *Stu) GetStuCommittee(p *dto.GetStuCommittee) (Committee *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Committee, err = stu.GetStuCommittee(p)
	res := new(bo.GetStuList)
	stuCommittee,  err := stu.GetStuCommittee(p)
	if err != nil {
		return nil, err
	}
	var getStuCommittee []*bo.GetStuList
	for _, stu := range *stuCommittee {
		getStuCommittee = append(getStuCommittee, &bo.GetStuList{
			Id:         stu.ID,
			Gender:    stu.Gender,
			Major:   stu.Major,
			Class:   stu.Class,
			Name:       stu.Name,
			Number:   stu.Number,
			Phone:   stu.Phone,
			Nation:   stu.Nation,
			Committee:   stu.Committee,
			StudentUnion:  stu.StudentUnion,
			State:   stu.State,
			PsychologicalWarning:  stu.PsychologicalWarning,
			Team:   stu.Team,
		})
	}
	res.Committee = p.Committee
	return
}
//GetStuMember 通过党员查询学生列表
func (e *Stu) GetStuMember(p *dto.GetStuMember) (Member *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Member, err = stu.GetStuMember(p)
	res := new(bo.GetStuList)
	stuMember,  err := stu.GetStuMember(p)
	if err != nil {
		return nil, err
	}
	var getStuMember []*bo.GetStuList
	for _, stu := range *stuMember {
		getStuMember = append(getStuMember, &bo.GetStuList{
			Id:         stu.ID,
			Gender:    stu.Gender,
			Major:   stu.Major,
			Class:   stu.Class,
			Name:       stu.Name,
			Number:   stu.Number,
			Phone:   stu.Phone,
			Nation:   stu.Nation,
			Committee:   stu.Committee,
			StudentUnion:  stu.StudentUnion,
			State:   stu.State,
			PsychologicalWarning:  stu.PsychologicalWarning,
			Team:   stu.Team,
		})
	}
	res.Member = p.Member
	return
}
//GetStuMember 通过党员查询学生列表
func (e *Stu) GetStuNation(p *dto.GetStuNation) (Nation *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Nation, err = stu.GetStuNation(p)
	res := new(bo.GetStuList)
	stuNation,  err := stu.GetStuNation(p)
	if err != nil {
		return nil, err
	}
	var getStuNation []*bo.GetStuList
	for _, stu := range *stuNation {
		getStuNation = append(getStuNation, &bo.GetStuList{
			Id:         stu.ID,
			Gender:    stu.Gender,
			Major:   stu.Major,
			Class:   stu.Class,
			Name:       stu.Name,
			Number:   stu.Number,
			Phone:   stu.Phone,
			Nation:   stu.Nation,
			Committee:   stu.Committee,
			StudentUnion:  stu.StudentUnion,
			State:   stu.State,
			PsychologicalWarning:  stu.PsychologicalWarning,
			Team:   stu.Team,
		})
	}
	res.Nation = p.Nation
	return
}
//GetStuMember 通过学生会查询学生列表
func (e *Stu) GetStuUnion(p *dto.GetStuUnion) (Union *[]models.SysStu, err error) {
	stu := new(models.SysStu)
	Union, err = stu.GetStuUnion(p)
	res := new(bo.GetStuList)
	stuUnion,  err := stu.GetStuUnion(p)
	if err != nil {
		return nil, err
	}
	var getStuUnion []*bo.GetStuList
	for _, stu := range *stuUnion {
		getStuUnion = append(getStuUnion, &bo.GetStuList{
			Id:         stu.ID,
			Gender:    stu.Gender,
			Major:   stu.Major,
			Class:   stu.Class,
			Name:       stu.Name,
			Number:   stu.Number,
			Phone:   stu.Phone,
			Nation:   stu.Nation,
			Committee:   stu.Committee,
			StudentUnion:  stu.StudentUnion,
			State:   stu.State,
			PsychologicalWarning:  stu.PsychologicalWarning,
			Team:   stu.Team,
		})
	}
	res.StudentUnion = p.Union
	return
}
//Scholarship 奖学金列表
func (e *Stu) Scholarship(p *dto.Scholarship) (Ship *[]models.SysShip, err error) {
	stu := new(models.SysShip)
	Ship, err = stu.Scholarship(p)
	res := new(bo.Scholarship)
	stuShip,  err := stu.Scholarship(p)
	if err != nil {
		return nil, err
	}
	var getShip []*bo.Scholarship
	for _, stu := range *stuShip {
		getShip = append(getShip, &bo.Scholarship{
			Time:              stu.Time,
			Level:			   stu.Level,
			Grade:             stu.Grade,
		})
	}
	res.Time = stu.Time
	res.Grade = stu.Grade
	res.Level = stu.Level
	return
}
// Downscholarship 奖学金下载
func (e *Stu) Downscholarship() (content io.ReadSeeker, err error) {
	var res []interface{}
	ship := new(models.Scholarship)
	shipList, err := ship.ShipListDownload()
	if err != nil {
		return
	}

	for _, ship := range shipList {
		res = append(res, &bo.Scholarship{
			Name:  ship.Name,
			Number:ship.Number,
			Class: ship.Class,
			Time:ship.Time,
			Level:ship.Level,
			Grade:ship.Grade,
		})
	}
	content = utils.ToExcel([]string{`姓名`, `学号`, `班级`, `时间`, `级别`, `等级`}, res)
	return
}
// Downinsegrals 获奖信息下载
func (e *Stu) Downinsegrals() (content io.ReadSeeker, err error) {
	var res []interface{}
	inse := new(models.Insegrals)
	inseList, err := inse.Downinsegrals()
	if err != nil {
		return
	}

	for _, inse := range inseList {
		res = append(res, &bo.Insegrals{
			Name:  inse.Name,
			Number:inse.Number,
			Class: inse.Class,
			Time:inse.Time,
			InName:inse.InName,
			Level:inse.Level,
			Insegrals:inse.Insegral,
			InPoints:inse.InPoints,
		})
	}
	content = utils.ToExcel([]string{`姓名`, `学号`, `班级`, `时间`, `名称`, `级别`, `等级`, `积分`}, res)
	return
}
// Downnoinsegrals 获奖信息下载
func (e *Stu) Downnoinsegrals() (content io.ReadSeeker, err error) {
	var res []interface{}
	noinse := new(models.Noinsegrals)
	noinseList, err := noinse.Downnoinsegrals()
	if err != nil {
		return
	}

	for _, noinse := range noinseList {
		res = append(res, &bo.Noinsegrals{
			Name:  noinse.Name,
			Number:noinse.Number,
			Class: noinse.Class,
			Time:noinse.Time,
			NoName:noinse.NoName,
			Level:noinse.Level,
			Nosegrals:noinse.Nosegral,
			NoPoints:noinse.NoPoints,
		})
	}
	content = utils.ToExcel([]string{`姓名`, `学号`, `班级`, `时间`, `名称`, `级别`, `等级`, `积分`}, res)
	return
}

func (e *Stu) Addpoint(userId int, p *dto.Addpoint) (err error) {
	ins := new(models.Insegral)
	ins.InName = p.InName
	ins.Level = p.Level
	ins.Studentid = p.Studentid
	ins.Insegral = p.Insegral
	ins.InPoints = p.InPoints
	ins.Time = p.Time
	err = ins.Addpoint()
	return
}
func (e *Stu) Delpoint(userId int, p *dto.Delpoint) (err error) {
	ins := new(models.Noinsegral)
	ins.NoName = p.NoName
	ins.Level = p.Level
	ins.Studentid = p.Studentid
	ins.Nosegral = p.Nosegral
	ins.NoPoints = p.NoPoints
	ins.Time = p.Time
	err = ins.Delpoint()
	return
}
func (e *Stu) Deladdpoint(p *dto.Deladdpoint)(delpoint *[]models.Insegral, err error) {
	ins := new(models.Insegral)
	err = ins.Deladdpoint(p)
	return
}






func (e *Stu) Add(p *dto.Add) (err error) {
	println(p.Number)
	ins := new(models.SysStu)
	one := new(models.Stu)
	one.Number = p.Number
	one.Phone = p.Phone
	one.Password = "123456"


	ins.QQ = p.QQ
	ins.VX = p.VX
	ins.Name = p.Name
	ins.Gender = p.Gender
	ins.Major = p.Major
	ins.Number = p.Number
	ins.Phone = p.Phone
	ins.NativePlace = p.NativePlace
	ins.Place = p.Place
	ins.Nation = p.Nation
	ins.Team = p.Team
	ins.Class = p.Class
	ins.Committee = p.Committee
	ins.Dormitory = p.Dormitory
	ins.StudentUnion = p.StudentUnion
	ins.PoliticalOutlook = p.PoliticalOutlook
	ins.WatchmanName = p.WatchmanName
	ins.WatchmanRelationship = p.WatchmanRelationship
	ins.WatchmanPhone = p.WatchmanPhone
	ins.State = p.State
	ins.Remarks = p.Remarks
	ins.Year = p.Year
	err = ins.Add()
	err = one.Add1()
	return
}


func (e *Stu) Login(c *gin.Context, p *dto.Login) (data *bo.Login, err error) {
	user := new(models.Login)
	user.Number = p.Number
	user.Password = p.Password
	if err = user.Login(); err != nil {
		return
	}
	return
}